package io.renren.modules.wyzs.mobile_BillAdapterMoudle;

import io.renren.modules.wyzs.mobile_EnumMoudle.BillStatus;
import io.renren.modules.wyzs.mobile_EnumMoudle.EnumDataExecType;
import java.util.*;
import java.text.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;


public class BillAdpater {
    public HashMap<String, OwnField> Fields = new HashMap<String, OwnField>();
    public HashMap<Integer, OwnEntry> Entrys = new HashMap<Integer, OwnEntry>();

    public void setBillStatus(BillStatus billStatus){
    	this._status=billStatus;
    }
    
    public BillStatus getBillStatus(){
    	return this._status;
    }
    
    public Integer getBillID(){
    	if(_data==null){
    		return 0;
    	}
    	return _data.getID();
    }
    
    public String getClassType(){
    	if(_temp==null){
    		return "";
    	}
    	return _temp.getClassType();
    }
    
    public String CharStamp;
    public String NewStamp;
    BillData _data;
    BillTemp _temp;
    BillStatus _status ;
    public BillAdpater(BillTemp temp)
    {
    	_temp = temp;
        ClearBing();
    }


    public void Fill(BillData data)
    {
        _data = data;
        NewStamp = data.getTimeStamp();
        if (data.getID() == 0)
        {
            _status = BillStatus.New;
        }
        else
        {
            _status = BillStatus.Edit;
        }
        CharStamp = data.getCharStamp();

        
        for (String key : Fields.keySet()) {
            if (data.getFields().containsKey(Fields.get(key).getFieldTemp().getID()))
            {
            	Fields.get(key).Init(data.getFields().get(Fields.get(key).getFieldTemp().getID()));
            }
        }
        
        for (OwnEntry entry: Entrys.values()) {

			if(data.getEntrys().containsKey(entry.Page)){
				BillEntry billEntry=data.getEntrys().get(entry.Page);
				for(BillEntryRow row :billEntry.getRows()){
                    OwnRow ownRow = new OwnRow();
                    ownRow.DataExecType = EnumDataExecType.None;
                    ownRow.ID = row.getID();
                    for(OwnColumn col : entry.Columns.values()){
                        if (row.getFields().containsKey(col.FieldTemp.getID())){
                            OwnField field = new OwnField(col.FieldTemp);
                            field.Init(row.getFields().get(col.FieldTemp.getID()));
                            ownRow.Fields.put(col.FieldTemp.getKey(), field);
                        }
                    }
                    
                    ownRow.setIndex(row.getIndex()); 
                    entry.Rows.add(ownRow);
				}
				

			}
		}

    }
    
    public void ClearBing()
    {
        Fields.clear();
        Entrys.clear();

        for (FieldTemp ft : _temp.GetFieldsByPage(0))
        {
            OwnField field = new OwnField(ft);
            Fields.put(ft.getKey(), field);
        }
        
        for (Integer key : _temp.getEntrys().keySet()) {
            
            OwnEntry ownEntry = new OwnEntry();
            ownEntry.Page = _temp.getEntrys().get(key).getPage();
            for (FieldTemp ft : _temp.GetFieldsByPage(ownEntry.Page))
            {
                OwnColumn col = new OwnColumn();
                col.FieldTemp = ft;
                ownEntry.Columns.put(ft.getKey(), col);
            }
            Entrys.put(key, ownEntry);
        }
    }

    public BillData New()
    {
        _status = BillStatus.New;
        _data = new BillData();
        NewStamp = _data.getNewStamp();

        for (String key : Fields.keySet()) {
            Fields.get(key).Init(null);
        }

        return _data;
    }



    /// <summary>
    /// �����Ҫ�������Ŀ
    /// </summary>
    /// <returns></returns>
    public BillSaver GetSaveItem()
    {
        BillSaver billSaver = new BillSaver();
        billSaver.setCalssType("Sales");
        _data=new BillData();
        Date now = new Date();
        SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
        _data.setTimeStamp(ft.format(now));
        Random random = new Random();
        billSaver.setID(0);
        //billSaver.setStatus(_status);
        billSaver.setCalssType(_temp.getClassType());
        billSaver.setTimeStamp(_data.getTimeStamp());
        billSaver.setCharStamp(_data.getCharStamp());
        billSaver.setNewStamp(_data.getNewStamp()); 

        for (String key : Fields.keySet()) {
            
            if (Fields.get(key).getFieldTemp().getNeedSave())
            {
                    FieldSaveValue value = new FieldSaveValue();
                    value.setNewValue(Fields.get(key).getValue());
                    value.setOldValue(Fields.get(key).getOldValue());
                    billSaver.getSaveItems().put(Fields.get(key).getFieldTemp().getID(), value);
                    billSaver.setNeedSave(true);
            }
        }
        
        for (Integer key : Entrys.keySet()) {
            BillEntrySaver entrySave = new BillEntrySaver();
            entrySave.setPage(Entrys.get(key).getPage()); 
            List<OwnRow> allEntryRows=new LinkedList<OwnRow>();
            allEntryRows.addAll(Entrys.get(key).Rows);
            allEntryRows.addAll(Entrys.get(key).DeleteRows);

            for (OwnRow row : allEntryRows) {
                BillRowSaver rowSave = null;
            	switch (row.DataExecType) {
				case None:
				case Update:
			        for (String Value : row.Fields.keySet()) {
                        if (row.Fields.get(Value).getIsEdit()&& row.Fields.get(Value).getFieldTemp().getNeedSave())
                        {
                            if (rowSave == null)
                            {
                                rowSave = new BillRowSaver();
                                rowSave.setIndex(EnumDataExecType.Update);
                            }
                            FieldSaveValue value = new FieldSaveValue();
                            value.setNewValue(row.Fields.get(Value).getValue()); 
                            value.setOldValue(row.Fields.get(Value).getOldValue());
                            rowSave.getFields().put(row.Fields.get(Value).getFieldTemp().getID(), value);
                        }
			        }

					break;
				case Insert:
                    rowSave = new BillRowSaver();
			        for (String Value : row.Fields.keySet()) {
                        if ( row.Fields.get(Value).getFieldTemp().getNeedSave())
                        {
                            FieldSaveValue value = new FieldSaveValue();
                            value.setNewValue(row.Fields.get(Value).getValue()); 
                            value.setOldValue(row.Fields.get(Value).getOldValue());
                            rowSave.getFields().put(row.Fields.get(Value).getFieldTemp().getID(), value);
                        }
			        }
			        rowSave.setIndex(EnumDataExecType.Insert);

					break;
                case Delete:
                    rowSave = new BillRowSaver();
                    rowSave.setIndex(EnumDataExecType.Delete);
                    break;
				}
            	
                if (rowSave != null)
                {
                    rowSave.setIndex(row.getIndex());
                    rowSave.setRowID(row.ID);
                    entrySave.getRows().add(rowSave);
                }
            }
            
            if (entrySave.getRows().size() > 0)
            {
            	billSaver.getSaveEntrys().put(entrySave.getPage(), entrySave);
            	billSaver.setNeedSave(true);
            }
     
        }
        return billSaver;
    }
}

